<?php

namespace app\admin\controller;

use app\admin\model\Column;
use think\Controller;
use app\admin\model\Module;

class ContentController extends BaseController
{
    public function index()
    {
        $column = (new Column())->leftJoin('w_module','w_column.module_id= w_module.module_id')->select()->toArray();
        $revert_column =[];
        foreach ($column as $item) {
            if ($item['column_type'] == 1 ){
                continue;
            }
            $revert_column[]  = $item;
        }
        $this->assign('column',$this->build_tree($revert_column,0));
        return $this->fetch();
    }

   public function build_tree($data,$pid){
        $html = '';
       foreach ($data as $item) {
           if ($item['column_parent'] == $pid){
               $html.= $pid  ? '<dd class="lgp-nav-item">' : '<li class="lgp-nav-item">';
               $html.= '<a href="javascript:void(0)" lgp-href="'.$item['control'].'/'.$item['method'].'?column_id='.$item['column_id'].'">';
               $html.= $item['column_name'];
               $html.= '</a>';
               $html.='<span class="lgp-op">';
               $html.='<i class="layui-icon layui-icon-add-1"></i>';
               $html.='<i class="layui-icon layui-icon-edit"></i>';
               $html.='</span>';
               $html.= $this->build_tree($data,$item['column_id']);
               $html.=  $pid  ? '</dd>' : '</li>';
           }
        }
       if ($pid){
           return $html?'<span class="layui-icon layui-icon-down dirs"></span><dl class="lgp-nav-child">'.$html.'</dl>':$html;
       }else{
           return $html?'<ul class="lgp-nav">'.$html.'</ul>':$html;
       }
   }
   public function add(){
       $status = (new Module())->save($this->request->param());
       $msg= '添加成功';
       $code=0;
       if (!$status){
        $msg ='添加失败';
        $code = 404;
       }
       return ['code'=>$code,'msg'=>$msg];

   }


}
